package com.gjh.xiaokabang2code.controller;

import com.gjh.xiaokabang2code.common.*;
import com.gjh.xiaokabang2code.model.request.LoginRt;
import com.gjh.xiaokabang2code.model.request.MailRt;
import com.gjh.xiaokabang2code.model.request.UpdateUserRt;
import com.gjh.xiaokabang2code.model.request.VerifyRt;
import com.gjh.xiaokabang2code.model.response.LoginRs;
import com.gjh.xiaokabang2code.model.response.SafetyUserRs;
import com.gjh.xiaokabang2code.model.response.VerifyRs;
import com.gjh.xiaokabang2code.service.CommonService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@RestController
@RequestMapping("/public")
//如果是dev环境，需要配置跨域请求是http://localhost:3000,如果是生产环境，需要配置跨域请求是https://www.xkb.icu/
@CrossOrigin(origins = "${cors.origins}", allowCredentials = "true")
public class CommonController {


    @Value("${cors.origins}")
    private String allowedOrigins;
    @Resource
    private CommonService commonService;

    @GetMapping("/lose")
    public BaseResponse<ArrayList<String>> loseList() throws Exception {
        ArrayList<String> list = commonService.loseList();
        return ResultUtils.success(list);
    }

    @GetMapping("/find")
    public BaseResponse<ArrayList<String>> findList() throws Exception {
        ArrayList<String> list = commonService.findList();
        return ResultUtils.success(list);
    }
    @GetMapping("/mail/safetyCode")
    public BaseResponse<Boolean> sendSafetyCode(HttpServletRequest httpServletRequest) {
        LoginRs loginUser = HttpSessionUtil.getLoginUser(httpServletRequest);
        if (loginUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录无法正常使用当前页面功能");
        }
        Boolean result = commonService.sendSafetyCode(loginUser);
        if(!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "系统内部严重错误");
        }
        return ResultUtils.success(true);
    }
    @PostMapping("/verify/safetyCode")
    public BaseResponse<String> verifySafetyCode(@RequestBody VerifyRt verifyRt, HttpServletRequest httpServletRequest) throws Exception {
        String verifyCode = verifyRt.getVerifyCode();
        if (verifyCode == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "登陆时学号为空(C1001)");
        }
        String uc = commonService.verifySafetyCode(verifyCode, httpServletRequest);
        if (uc == null) {
            throw new BusinessException(ErrorCode.DATA_NULL, "返回登录凭证为空（C1002）");
        }
        return ResultUtils.success(uc);
    }

    @GetMapping("/verify")
    public BaseResponse<String> verify(HttpServletRequest httpServletRequest) throws Exception {
        VerifyRs safetyUser = HttpSessionUtil.getSafetyUser(httpServletRequest);
        if (safetyUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未进行二级安全验证，无法正常使用当前页面功能");
        }
        String uc = safetyUser.getUc();
        if (uc == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未进行二级安全验证，无法正常使用当前页面功能");
        }
        return ResultUtils.success(uc);
    }
}
